package com.dg.river.core.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.dg.river.R;
import com.dg.river.core.util.TextUtil;

/**
 * @作者：Hututu
 * @时间：2023/4/24 15:48
 * @描述：内容描述
 * @修改：修改内容
 * @Version:1.0
 */
public class StatusLayout extends FrameLayout {

    private View view;

    private LinearLayout llStatus;
    private ImageView ivIcon;
    private TextView textView;

    private LinearLayout llLoading;

    private LinearLayout llNetWorkError;
    private TextView tvRetry;

    // 展示类型
    private ShowType showType;

    public interface OnRetryListener {
        void onRetry();
    }

    // 重试监听
    public OnRetryListener onRetryListener;

    public StatusLayout(@NonNull Context context) {
        super(context);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StatusLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initLayout() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.layout_status, this, false);
        llStatus = view.findViewById(R.id.llStatus);
        ivIcon = view.findViewById(R.id.ivIcon);
        textView = view.findViewById(R.id.textView);

        llLoading = view.findViewById(R.id.llLoading);

        llNetWorkError = view.findViewById(R.id.llNetWorkError);
        tvRetry = view.findViewById(R.id.tvRetry);

        addView(view);
    }

    /**
     * 初始化布局
     */
    public void init() {
        if (view == null) {
            initLayout();
        }
        if (view != null) {
            if (view.getVisibility() == VISIBLE) {
                return;
            }
            view.setVisibility(VISIBLE);
        }
    }

    /**
     * 释放布局
     */
    public void release() {
        if (view == null || view.getVisibility() == GONE) {
            return;
        }
        view.setVisibility(GONE);
    }

    public enum ShowType {
        LOADING, // 加载中
        NET_WORK_ERROR, // 网络异常
        PLACEHOLDER // 占位图（空数据、加载失败）
    }

    public void setShowType(ShowType showType) {
        this.showType = showType;
        switch (showType) {
            case LOADING:
                llLoading.setVisibility(VISIBLE);
                llStatus.setVisibility(GONE);
                llNetWorkError.setVisibility(GONE);
                break;
            case PLACEHOLDER:
                llStatus.setVisibility(VISIBLE);
                llLoading.setVisibility(GONE);
                llNetWorkError.setVisibility(GONE);
                break;
            case NET_WORK_ERROR:
                llNetWorkError.setVisibility(VISIBLE);
                llLoading.setVisibility(GONE);
                llStatus.setVisibility(GONE);
                break;
        }
    }

    /**
     * 设置展示的文字
     *
     * @param text
     */
    public void setText(String text) {
        if (showType != ShowType.PLACEHOLDER) {
            return;
        }
        if (textView == null) {
            return;
        }
        textView.setText(TextUtil.isEmpty(text) ? "暂无内容" : text);
    }

    /**
     * 设置展示的logo
     *
     * @param drawableId
     */
    public void setIcon(@DrawableRes int drawableId) {
        if (showType != ShowType.PLACEHOLDER) {
            return;
        }
        if (ivIcon == null) {
            return;
        }
        if (drawableId == 0) {
            ivIcon.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.icon_empty_ic_2));
        } else {
            Drawable drawable = ContextCompat.getDrawable(getContext(), drawableId);
            if (drawable == null) {
                return;
            }
            ivIcon.setImageDrawable(drawable);
        }
    }

    /**
     * 设置重试监听
     *
     * @param onRetryListener
     */
    public void setRetryListener(OnRetryListener onRetryListener) {
        this.onRetryListener = onRetryListener;
        tvRetry.setOnClickListener(v -> {
            if (onRetryListener != null) {
                onRetryListener.onRetry();
            }
        });
    }
}
